package com.aduerr.v1shouyin.mvp.adpter;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.unioncard.UnionTranscationDetailActivity;
import com.yeahka.shouyintong.sdk.info.TradeInfo;

import java.util.List;

/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2019/7/17 16:34
 * @change
 * @chang time
 * @class describe
 */

public class UnionTranscationAdapter extends RecyclerView.Adapter<UnionTranscationAdapter.ViewHolder> {

    private Context context;
    private List<TradeInfo> lists;

    public UnionTranscationAdapter(Context context, List<TradeInfo> lists) {
        this.context = context;
        this.lists = lists;
    }


    public void addData(List<TradeInfo> lists) {
        for(TradeInfo lb : lists) {
            this.lists.add(lb);
        }
        notifyDataSetChanged();
    }

    public void updateData(List<TradeInfo> lists) {
        this.lists = lists;
        notifyDataSetChanged();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v =
                LayoutInflater.from(parent.getContext()).inflate(R.layout.item_union_transcation, parent, false);
        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        TradeInfo tradeInfo = lists.get(position);
        holder.tvDate.setText(tradeInfo.getDate());
        holder.tvTime.setText(new StringBuffer(tradeInfo.getTime().substring(0, 4)).insert(2, ":"));
        holder.tvReferenceId.setText(TextUtils.isEmpty(tradeInfo.getReferenceNo()) ?
                "已取消" : tradeInfo.getReferenceNo());
        if(tradeInfo.getTransactionType() == 5) {
            //刷卡交易
            holder.tvType.setText("消费");
            holder.tvType.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        } else if(tradeInfo.getTransactionType() == 4) {
            //刷卡退款
            holder.tvType.setText("退款");
            holder.tvType.setTextColor(context.getResources().getColor(R.color.l_color));
        } else if(tradeInfo.getTransactionType() == 10) {
            //预授权
            holder.tvType.setText("预授权");
            holder.tvType.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        } else if(tradeInfo.getTransactionType() == 11) {
            //预授权撤销
            holder.tvType.setText("预授权撤销");
            holder.tvType.setTextColor(context.getResources().getColor(R.color.l_color));
        } else if(tradeInfo.getTransactionType() == 20) {
            //预授权完成
            holder.tvType.setText("预授权完成");
            holder.tvType.setTextColor(context.getResources().getColor(R.color.colorPrimary));
        } else if(tradeInfo.getTransactionType() == 21) {
            //预授权完成撤销
            holder.tvType.setText("预授权完成撤销");
            holder.tvType.setTextColor(context.getResources().getColor(R.color.l_color));
        }

        holder.tvMoney.setText((Float.parseFloat(tradeInfo.getAmount() + "") / 100) + "");
        holder.itemView.setOnClickListener(v -> {
            Intent intent = new Intent(context, UnionTranscationDetailActivity.class);
            intent.putExtra("tradeInfo", tradeInfo);
            context.startActivity(intent);
        });

    }

    @Override
    public int getItemCount() {
        return lists == null ? 0 : lists.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        TextView tvDate, tvTime, tvType, tvReferenceId, tvMoney;

        public ViewHolder(View itemView) {
            super(itemView);
            tvDate = (TextView) itemView.findViewById(R.id.tv_date);
            tvTime = (TextView) itemView.findViewById(R.id.tv_time);
            tvType = (TextView) itemView.findViewById(R.id.tv_type);
            tvReferenceId = (TextView) itemView.findViewById(R.id.tv_reference_id);
            tvMoney = (TextView) itemView.findViewById(R.id.tv_money);
        }
    }
}
